#include "InputManager.h"
#ifdef _WINDOWS
	#include <Windows.h>
#endif

InputManager::InputManager(std::shared_ptr<sf::RenderWindow> window)
{
	_window = window;
	for (unsigned int i = 0; i < sf::Keyboard::KeyCount; i++)
	{
		_keys[i] = false;
		_lastKeys[i] = false;
	}

	for (unsigned int i = 0; i < sf::Mouse::ButtonCount; i++)
	{
		_mouseButtons[i] = false;
		_lastMouseButtons[i] = false;
	}

	for (unsigned int i = 0; i < sf::Joystick::Count; i++)
	{
		if (sf::Joystick::isConnected(i))
			_joysticks.push_back(CJoystick(i));
		else
			break;
	}
}


InputManager::~InputManager(void){}

bool InputManager::capture()
{
	memcpy(_lastKeys, _keys, sizeof(bool) * sf::Keyboard::KeyCount);
	memcpy(_lastMouseButtons, _mouseButtons, sizeof(bool) * sf::Mouse::ButtonCount);
	if (!_joysticks.empty())
	{
		for (auto it = _joysticks.begin(); it != _joysticks.end(); it++)
			(*it).update();
	}

	sf::Event event;
	while (_window->pollEvent(event))
	{
		switch (event.type)
		{
		case sf::Event::Closed:
			{
				#ifdef _WINDOWS
					int choice = MessageBox(NULL, "Are you sure you want to quit?", "Quit", MB_YESNO | MB_ICONQUESTION);
					if (choice == IDYES)
						_window->close();
				#endif
				return false;
				break;
			}
		case sf::Event::KeyPressed:
				_keys[event.key.code] = true;
				break;
		case sf::Event::KeyReleased:
				_keys[event.key.code] = false;
				break;
		case sf::Event::MouseButtonPressed:
				_mouseButtons[event.mouseButton.button] = true;
				break;
		case sf::Event::MouseButtonReleased:
				_mouseButtons[event.mouseButton.button] = false;
				break;
		/*case sf::Event::JoystickButtonPressed:
				_joysticks[event.joystickButton.joystickId]._buttons[event.joystickButton.button] = true;
				break;
		case sf::Event::JoystickButtonReleased:
				_joysticks[event.joystickButton.joystickId]._buttons[event.joystickButton.button] = false;
				break;*/
		case sf::Event::MouseMoved:
				_mousePosition.x = event.mouseMove.x;
				_mousePosition.y = event.mouseMove.y;
				break;
		}
	}
	return true;
}

bool InputManager::isMouseButtonPressed(unsigned int button) const
{
	if (!_lastMouseButtons[button] && _mouseButtons[button])
		return true;
	return false;
}

bool InputManager::isMouseButtonReleased(unsigned int button) const
{
	if (_lastMouseButtons[button] && !_mouseButtons[button])
		return true;
	return false;
}

bool InputManager::isKeyPressed(unsigned int key) const
{
	if (!_lastKeys[key] && _keys[key])
		return true;
	return false;
}

bool InputManager::isKeyReleased(unsigned int key) const
{
	if (_lastKeys[key] && !_keys[key])
		return true;
	return false;
}

bool InputManager::isJoystickBtnDown(unsigned int joystickId, unsigned int button)
{
	if ( (joystickId < 0 || joystickId > _joysticks.size() - 1) || _joysticks.empty())
		return false;
	return _joysticks[joystickId]._buttons[button]; 
}

bool InputManager::isJoystickBtnPressed(unsigned int joystickId, unsigned int button)
{
	if ( (joystickId < 0 || joystickId > _joysticks.size() - 1) || _joysticks.empty())
		return false;

	if (!_joysticks[joystickId]._lastButtons[button] && _joysticks[joystickId]._buttons[button])
		return true;
	return false;
}

bool InputManager::isJoystickBtnReleased(unsigned int joystickId, unsigned int button)
{
	if ( (joystickId < 0 || joystickId > _joysticks.size() - 1) || _joysticks.empty())
		return false;

	if (_joysticks[joystickId]._lastButtons[button] && !_joysticks[joystickId]._buttons[button])
		return true;
	return false;
}